﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Threading;
using Northwind.Model;
using Northwind.Repositories;
using Northwind.Repositories.Entity;

namespace jEditable.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        #region Fields
        IEmployeesRepository _employeesRepository;
        #endregion

        #region Constructors
        public HomeController()
            : this(new EmployeesRepository())
        {
        }

        public HomeController(IEmployeesRepository employeesRepository)
        {
            _employeesRepository = employeesRepository;
        }
        #endregion

        #region Actions
        public ActionResult Employees()
        {
            return View(_employeesRepository.FindAll());
        }

        public ActionResult Employee(int employeeKey)
        {
            return View(_employeesRepository.FindByKey(employeeKey));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Employee(Employee modified)
        {
            Employee original = _employeesRepository.FindByKey(modified.Id);
            original.Title = modified.Title;
            original.Address = modified.Address;
            original.PostalCode = modified.PostalCode;
            original.City = modified.City;
            original.Country = modified.Country;
            original.Notes = modified.Notes;
            _employeesRepository.SaveChanges();

            return new EmptyResult();
        }
        #endregion
    }
}
